cmake_minimum_required(VERSION 3.10)
project(IPU_ProtoNet_Classifier)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置交叉编译工具链 - 匹配SSC377配置
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(PREFIX "arm-linux-gnueabihf-sigmastar-11.1.0-")
set(CMAKE_C_COMPILER ${PREFIX}gcc)
set(CMAKE_CXX_COMPILER ${PREFIX}g++)

# 设置芯片和平台配置
set(CHIP "i6c")
set(TOOLCHAIN "glibc")
set(TOOLCHAIN_VERSION "11.1.0")
set(LINK_TYPE "static")

# 设置路径
set(THIRD_PARTY_PATH ${CMAKE_CURRENT_SOURCE_DIR}/third_party)

# SDK相关路径 - 直接从third_party目录获取
set(SDK_PROJECT_PATH ${THIRD_PARTY_PATH}/sdk/project/release)
set(SDK_KERNEL_PATH ${THIRD_PARTY_PATH}/sdk/kernel)
set(MI_LIBS_PATH ${SDK_PROJECT_PATH}/chip/${CHIP}/ipc/common/${TOOLCHAIN}/${TOOLCHAIN_VERSION}/mi_libs/${LINK_TYPE})
set(SIGMA_LIBS_PATH ${SDK_PROJECT_PATH}/chip/${CHIP}/sigma_common_libs/${TOOLCHAIN}/${TOOLCHAIN_VERSION}/${LINK_TYPE})

# 编译选项 - 匹配SSC377的GCC选项，启用异常处理
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -mthumb -pipe -fPIC -mfpu=neon-vfpv4 -mfloat-abi=hard -fexceptions")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCONFIG_SIGMASTAR_CHIP_I6C=1 -DLINUX_OS -std=c++17")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "-g")

# 包含目录 - 仅包含必要的头文件
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    
    # SDK包含路径
    ${SDK_PROJECT_PATH}/include
    ${SDK_KERNEL_PATH}/drivers/sstar/include
    
    # 第三方库
    ${THIRD_PARTY_PATH}/include
    ${THIRD_PARTY_PATH}/opencv/include
    ${THIRD_PARTY_PATH}/opencv/include/opencv4
)

# 查找SDK的所有include目录
file(GLOB_RECURSE SDK_PROJECT_INCLUDE_DIRS "${SDK_PROJECT_PATH}/*/include")
file(GLOB_RECURSE SDK_KERNEL_INCLUDE_DIRS "${SDK_KERNEL_PATH}/*/include")
include_directories(${SDK_PROJECT_INCLUDE_DIRS} ${SDK_KERNEL_INCLUDE_DIRS})

# 链接库目录 - 按照AIEngine opencv.mk的配置
link_directories(
    ${MI_LIBS_PATH}
    ${SIGMA_LIBS_PATH}
    ${THIRD_PARTY_PATH}/opencv/static_lib_9.1.0
    ${THIRD_PARTY_PATH}/opencv/static_lib_9.1.0/opencv4/3rdparty
)

# 源文件
set(SOURCES
    src/ipu_model_wrapper.cpp
    src/ipu_image_preprocessor.cpp
    src/ipu_protonet_classifier.cpp
    src/main.cpp
)

# 创建可执行文件
add_executable(ipu_protonet_classifier ${SOURCES})

# 设置链接选项 - 匹配SSC377的链接优化
target_link_libraries(ipu_protonet_classifier -Wl,--gc-sections)

# 链接库 - 严格按照AIEngine OpenCV.mk的链接顺序，但不包含AIEngine依赖
target_link_libraries(ipu_protonet_classifier
    # 基础系统库
    pthread
    
    # MI库 - 按照依赖顺序
    mi_ipu
    mi_common
    mi_sys
    
    # CAM包装库
    cam_fs_wrapper
    cam_os_wrapper
    
    # OpenCV静态库 - 按照opencv.mk的顺序
    opencv_imgcodecs
    opencv_imgproc  
    opencv_core
    
    # OpenCV第三方依赖库 - 按照opencv.mk的精确顺序和命名
    ittnotify
    libjasper
    libjpeg-turbo
    libpng
    libtiff
    libwebp
    tegra_hal
    zlib
    
    # 系统库
    dl
    rt
    m
)

# 设置输出目录
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/build/bin)

# 创建输出目录
file(MAKE_DIRECTORY ${EXECUTABLE_OUTPUT_PATH})

# 安装目标
install(TARGETS ipu_protonet_classifier
    RUNTIME DESTINATION bin
)

# 打印配置信息
message(STATUS "IPU ProtoNet Classifier配置:")
message(STATUS "  工具链前缀: ${PREFIX}")
message(STATUS "  目标芯片: ${CHIP}")
message(STATUS "  MI库路径: ${MI_LIBS_PATH}")
message(STATUS "  OpenCV路径: ${THIRD_PARTY_PATH}/opencv")
message(STATUS "  输出路径: ${EXECUTABLE_OUTPUT_PATH}")